<?php

class PAYawalExcelReader extends PAExcelReader{
  
  private $dataRows = array();
  private $headers = array();
  private $headersOffset = array();
  private $options = array(
    "symbolsHeader" => "Kod",
    "quantityHeader" => "Ilość",
    "lengthHeader" => "Długość"
  );
  
  public function __construct($fileName = null, $options = array()){
    parent::__construct($fileName);
    $this->calculateFields();
    $this->setOptions($options);
  }
  
  private function setOptions($options){
    if(count($options)){
      foreach($options as $key => $value){
        $this->options[$key] = $value;
      }
    }
  }
  
  /**
   * Odnajduje: nagłówki kolumn, współrzędne wiersza nagłówków oraz wiersze
   * danych o akcesoriach i profilach
   */
  private function calculateFields(){
    foreach($this->getData() as $rowKey => $row){
      $rowHeaders = array();
      $size = count($row);
      foreach($row as $cellKey => $cell){
        if(!empty($cell)){
          if(!is_numeric($cell)) $rowHeaders[] = ucfirst($cell);
        }
      }
      if(count($this->headers)){
        if(!count($this->headersOffset)){
          foreach($this->headers as $key => $header){
            $this->headersOffset[$header] = array("x" => $key, "y" => $rowKey);
          }
        }
        // Dodaj do tablicy danych kolumny: kod, ilość, długość
        $symbol = $this->headersOffset[$this->options["symbolsHeader"]]["x"];
        $quantity = $this->headersOffset[$this->options["quantityHeader"]]["x"];
        $length = $this->headersOffset[$this->options["lengthHeader"]]["x"];
        
        $this->dataRows[$row[$symbol]] = array(
          $this->options["quantityHeader"] => $row[$quantity],
          $this->options["lengthHeader"] => $row[$length]
        );
      }
      if($size == count($rowHeaders)) $this->headers = $rowHeaders;
    }
  }
  
  private function getHeaderOffset($header = null){
    if(!is_null($header)){
      $xOffset = 0;
      $yOffset = 0;
      foreach($this->getData() as $rowKey => $row){
        foreach($row as $cellKey => $cell){
          if($header === $cell){
            $xOffset = $cellKey;
            $yOffset = $rowKey;
          }
        }
      }
      return array("x" => $xOffset,"y" => $yOffset);
    }
    else throw new InvalidArgumentException("Błędna nazwa nagłówka.");
  }
  
  /**
   * Zwraca nagłówki kolumn
   */
  public function getHeaders(){
    return $this->headers;
  }
  
  public function getLength($symbol)
  {
    $data = $this->getDataRows();
    return $data[$symbol][$this->options["lengthHeader"]];
  }
  
  /**
   * Zwraca wiersze z poszczególnymi akcesoriami i profilami z zamówienia,
   * bez nagłówka
   */
  public function getDataRows(){
    return $this->dataRows;
  }
  
  
  /**
   * Zwraca symbole występujące w zamówieniu
   */
  public function getSymbols($symbolsHeader = ""){
    $header = !empty($symbolsHeader) ? $symbolsHeader : $this->options["symbolsHeader"];
    $offset = $this->getHeaderOffset($header);
    $symbols = array();
    foreach($this->getDataRows() as $rowKey => $row){
      foreach($row as $cellKey => $cell){
        if($offset["x"] == $cellKey) $symbols[] = $cell;
      }
    }
    return $symbols;
  }
  
  /**
   * Zwraca ilości z zamówienia w formie tablicy:
   * $tablica = { "symbol_1" => ilość_1, ..., "symbol_n" => ilość_n }
   * Jeśli tablica $symbols jest pusta zwracana jest ilość dla każdego wiersza,
   * w innym przypadku ilości dla symboli określnych w $symbols
   */
  public function getQuantities($symbols = array(), $quantityHeader = "", $symbolsHeader = ""){
    // Nagłówek dla kolumny 'ilość'
    $header = !empty($quantityHeader) ? $quantityHeader : $this->options["quantityHeader"];
    $offset = $this->getHeaderOffset($header);
    
    // Nagłówek dla kolumny 'kod' lub 'symbol'
    $sHeader = !empty($symbolsHeader) ? $symbolsHeader : $this->options["symbolsHeader"];
    $sOffset = $this->getHeaderOffset($sHeader);
    
    $quantities = array();
    if(!count($symbols)){
      foreach($this->getDataRows() as $symbol => $data){
        $quantities[$symbol] = $data[$this->options["quantityHeader"]];
      }
    }
    else{
      foreach($this->getDataRows() as $symbol => $data){
        foreach($symbols as $querySymbol){
          if($symbol === $querySymbol) $quantities[$symbol] = $data[$this->options["quantityHeader"]];
        }
      }
    }
    return $quantities;
  }
}
